package logic

import (
	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	define "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/models"
	"context"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLeaveWordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLeaveWordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLeaveWordLogic {
	return &AddLeaveWordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLeaveWordLogic) AddLeaveWord(in *UserAction.UserActionRequest) (*UserAction.CommonResponse, error) {
	var leaveWord models.LeaveWord
	userId := int(in.UserId)
	content := in.Content
	if len(content) > define.LEAVE_WORD_CONTENT_LENGTH {
		return &UserAction.CommonResponse{
			Code: int64(HttpCode.PARAM_ERROR),
			Msg:  "留言内容不能超过" + strconv.Itoa(define.LEAVE_WORD_CONTENT_LENGTH) + "个字符",
		}, nil
	}
	leaveWord.Content = content
	leaveWord.UserId = userId
	err := models.Db.Model(new(models.LeaveWord)).Create(&leaveWord).Error
	if err != nil {
		return &UserAction.CommonResponse{
			Code: int64(HttpCode.SUCCESS),
		}, nil
	}
	return &UserAction.CommonResponse{
		Code: int64(HttpCode.SUCCESS),
	}, nil

}
